export default (ujt: any) => {
    const { $fn, $model, $global, $ws } = ujt;
    if (!$ws?.beat) {
        $ws.beat = {};
    }
    function pingPong(wstype: string = 'web', ptm: number = 0) {
        if (ptm || $global.sys?.tmbeat) {
            $ws.beat['bt' + wstype] = setInterval(() => {
                $ws.beat['btm' + wstype] = setTimeout(() => {
                    // 检测到心跳超时或断线
                    clearInterval($ws.beat['bt' + wstype]);
                    $ws.beat['bt' + wstype] = null;
                    $fn.exit();
                    // console.log(wstype, '断线了');
                }, (ptm || $global.sys?.tmbeat) * 1.5);

                $fn.ws[wstype].wsApi('ping').then((res: any) => {
                    // console.log(666.006, res);
                    if (res === 'pong') {
                        clearTimeout($ws.beat['btm' + wstype]);
                        $ws.beat['btm' + wstype] = null;
                    }
                });
            }, ptm || $global.sys?.tmbeat);
        }
    }
    return { pingPong };
};
